package com.syh.jn.scm.infrastructure.repository.impl.downstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.downstream.entity.base.DownBankAccount;
import com.syh.jn.scm.domain.downstream.repository.base.DownBankAccountRepository;
import com.syh.jn.scm.dto.downstream.base.DownBankAccountVo;
import com.syh.jn.scm.infrastructure.repository.model.base.BusinessEntityBankAccountModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 银行账户管理
 */
@Repository
public class DownBankAccountRepositoryImpl extends AbstractAggregateRepository<DownBankAccount, BusinessEntityBankAccountModel, Long> implements DownBankAccountRepository {

  @Override
  public Long add(DownBankAccount downBankAccount) {
    BusinessEntityBankAccountModel businessEntityBankAccountModel = new BusinessEntityBankAccountModel();
    ExtBeanUtils.copyProperties(downBankAccount, businessEntityBankAccountModel);
    businessEntityBankAccountModel.setTerminalType("DOWNSTREAM");
    return (Long) this.hibernateTemplate.save(businessEntityBankAccountModel);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.getCount(wrapper);
  }

  /**
   * 内部方法，所有列表查询都调用本方法 改方法是Hibernate实现 注意增删改 业务必须调用该接口数据 不能用view后缀的数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected List<DownBankAccount> superFindListPage(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.superFindListPage(wrapper, startRow, count);
  }

  /**
   * 内部方法，所有Sql执行的列表查询都调用本方法 改方法是 Mybatis 实现 注意增删改 业务不能调用该接口数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected <T> List<T> superFindListPageView(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.superFindListPageView(wrapper, startRow, count);
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(BusinessEntityBankAccountModel.class, "bankAccount", null)
      );
      FROM("T_BASE_BUSINESS_ENTITY_BANK_ACCOUNT bankAccount");
      INNER_JOIN("T_BASE_DOWNSTREAM downStream on bankAccount.BUSINESS_ENTITY_ID = downStream.ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return DownBankAccountVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("id", "bankAccount.ID");
    params.put("businessEntityId", "bankAccount.BUSINESS_ENTITY_ID");
    params.put("accountBank", "bankAccount.ACCOUNT_BANK");
    params.put("accountBranchBank", "bankAccount.ACCOUNT_BRANCH_BANK");
    params.put("status", "bankAccount.STATUS");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }
}
